﻿@page "/IdentityResources/Add"
@inject IIdentityResourceAppService IdentityResourcesService
@inject NavigationManager NavigationManager
@inject IOptions<IdentityServerAdminOptions> IdentityServerAdminOptions
@inject IObjectMapper ObjectMapper
@attribute [Authorize(IdentityServerAdminPermissions.IdentityResource.Create)]
<Card>
    <CardBody>
        <Button>
            <NavLink href="/IdentityServer/IdentityResources">Go Back</NavLink>
        </Button>
    </CardBody>
</Card>
<br />

@if (create != null)
{
    <Card>
        <CardBody>
            <Validations @ref="validations" Model="create" Mode="ValidationMode.Manual">
                <Validation>
                    <Field Horizontal="true">
                        <FieldLabel ColumnSize="ColumnSize.Is3">Name</FieldLabel>
                        <FieldBody ColumnSize="ColumnSize.Is9">
                            <Validation>
                                <TextEdit @bind-Text="create.Name"></TextEdit>
                            </Validation>
                        </FieldBody>
                    </Field>
                </Validation>
                <Validation>
                    <Field Horizontal="true">
                        <FieldLabel ColumnSize="ColumnSize.Is3">DisplayName</FieldLabel>
                        <FieldBody ColumnSize="ColumnSize.Is9">
                            <Validation>
                                <TextEdit @bind-Text="create.DisplayName"></TextEdit>
                            </Validation>
                        </FieldBody>
                    </Field>
                </Validation>
                <Validation>
                    <Field Horizontal="true">
                        <FieldLabel ColumnSize="ColumnSize.Is3">Description</FieldLabel>
                        <FieldBody ColumnSize="ColumnSize.Is9">
                            <Validation>
                                <TextEdit @bind-Text="create.Description"></TextEdit>
                            </Validation>
                        </FieldBody>
                    </Field>
                </Validation>
                <Validation>
                    <Field Horizontal="true">
                        <FieldLabel ColumnSize="ColumnSize.Is3">Enabled</FieldLabel>
                        <FieldBody ColumnSize="ColumnSize.Is9">
                            <Validation>
                                <Switch @bind-Checked="create.Enabled"></Switch>
                            </Validation>
                        </FieldBody>
                    </Field>
                </Validation>
                <Validation>
                    <Field Horizontal="true">
                        <FieldLabel ColumnSize="ColumnSize.Is3">Required</FieldLabel>
                        <FieldBody ColumnSize="ColumnSize.Is9">
                            <Validation>
                                <Switch @bind-Checked="create.Required"></Switch>
                            </Validation>
                        </FieldBody>
                    </Field>
                </Validation>
                <Validation>
                    <Field Horizontal="true">
                        <FieldLabel ColumnSize="ColumnSize.Is3">Emphasize</FieldLabel>
                        <FieldBody ColumnSize="ColumnSize.Is9">
                            <Validation>
                                <Switch @bind-Checked="create.Emphasize"></Switch>
                            </Validation>
                        </FieldBody>
                    </Field>
                </Validation>
                <Validation>
                    <Field Horizontal="true">
                        <FieldLabel ColumnSize="ColumnSize.Is3">ShowInDiscoveryDocument</FieldLabel>
                        <FieldBody ColumnSize="ColumnSize.Is9">
                            <Validation>
                                <Switch @bind-Checked="create.ShowInDiscoveryDocument"></Switch>
                            </Validation>
                        </FieldBody>
                    </Field>
                </Validation>
                <Validation>
                    <Field Horizontal="true">
                        <FieldLabel ColumnSize="ColumnSize.Is3">UserClaims</FieldLabel>
                        <FieldBody ColumnSize="ColumnSize.Is9">
                            <Validation>
                                <TagsInput @bind-Tags="create.UserClaims" OptionsLabel="Suggestions" Options="IdentityServerAdminOptions.Value.ApiResourceUserClaimSuggestions"></TagsInput>
                            </Validation>
                        </FieldBody>
                    </Field>
                </Validation>
            </Validations>
        </CardBody>
        <CardFooter>
            <Button Color="Color.Primary"
                    Type="ButtonType.Submit"
                    Clicked="Create">
                Add
            </Button>
            <Button Color="Color.Info"
                    Type="ButtonType.Reset">
                Reset
            </Button>
        </CardFooter>
    </Card>
}

@code {
    private CreateIdentityResourceInputDto create = new CreateIdentityResourceInputDto();
    private Validations validations { get; set; }
    private bool isAdding { get; set; }
    private async Task Create()
    {
        if (validations.ValidateAll())
        {
            isAdding = true;
            await IdentityResourcesService.CreateAsync(create);
            isAdding = false;
            create = new CreateIdentityResourceInputDto();
            NavigationManager.NavigateTo("/IdentityServer/IdentityResources");
        }
    }
}
